//
//  LXMallShareController.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/11.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXMallShareController.h"

//#import "LXMallRulerIntroduceController.h"

#import "LXMallShareView.h"

@interface LXMallShareController (){
    UIView * _containerView1;
    UIView * _containerView2;
}

@property(nonatomic,strong)LXMallShareView * shareView;

@property(nonatomic,strong)UIImageView * qrcodeImageView;

@property(nonatomic,strong)UILabel * invitedCode;

@end

@implementation LXMallShareController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(@"#EEEEEE");
    [self setUpNavi];

    [self setUpShareView];
    
    [self setUpView];
    
    [self xw_registerBackInteractiveTransitionWithDirection:XWInteractiveTransitionGestureDirectionRight transitonBlock:^(CGPoint startPoint) {
        [self.navigationController popViewControllerAnimated:YES];
    } edgeSpacing:100];
}

-(void)setUpShareView{

    LXMallShareView * shareView= [[LXMallShareView alloc] initWithFrame:CGRectMake(0, ScreenSize.height, ScreenSize.width, 200)];
    self.shareView = shareView;
    shareView.shareMethon = ^(ShareMethon methon){
        switch (methon) {
            case ShareMethonWechat:
            
            case ShareMethonWechatFriend:{
                SendMessageToWXReq * sendReq = [[SendMessageToWXReq alloc] init];
                sendReq.bText = NO;
                sendReq.scene = methon;
                WXMediaMessage * urlMessege = [WXMediaMessage message];
                urlMessege.title = @"扫我加入绿稀";
                urlMessege.description = @"扫我扫我扫我扫我扫我扫我扫我扫我扫我扫我。。。。";
                [urlMessege setThumbImage:[UIImage imageNamed:@"erwei"]];
                WXWebpageObject * webObj = [WXWebpageObject object];
                webObj.webpageUrl = @"http://appl.lx139.com/";
                urlMessege.mediaObject = webObj;
                sendReq.message = urlMessege;
                sendReq.openID = WeiXin_AppKey;
                [WXApi sendReq:sendReq];
            }break;
                
            case ShareMethonQQ:{
                
            }break;
                
            case ShareMethonQzone:{
                
            }break;
                
            case ShareMethonWeiBo:{
                
            }break;
             
            case ShareMethonCancel:{
                [self.shareView hidden];
            }break;
            default:
                break;
        }
    
    };
    
}

-(void)setUpView{
    UIView * containerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Ration_Share_width(326), Ration_Share_height(403))];
    [self.view addSubview:containerView1];
    containerView1.layer.borderWidth = 1;
    containerView1.layer.borderColor = HexColor(@"#3CB034").CGColor;
    containerView1.centerX = self.view.width * 0.5;
    containerView1.centerY = self.view.height * 0.5-64;
    containerView1.backgroundColor = HexColor(@"#EEEEEE");
    _containerView1 = containerView1;
    UIView * containerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerView1.width-globalMargin, containerView1.height - globalMargin)];

    containerView2.layer.borderWidth = 1;
    containerView2.layer.borderColor = HexColor(@"#3CB034").CGColor;
    containerView2.x = globalMargin*0.5;
    containerView2.y = globalMargin*0.5;
    containerView1.backgroundColor = [UIColor whiteColor];
    [containerView1 addSubview:containerView2];
    _containerView2 = containerView2;
    
    UIImageView * qrcodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Ration_Share_width(177), Ration_Share_height(177))];
    qrcodeImageView.image = [UIImage imageNamed:@"erwei"];
    qrcodeImageView.centerX = containerView2.width * 0.5;
    qrcodeImageView.y = (containerView2.width - qrcodeImageView.width)*0.5;
    
    [containerView2 addSubview:qrcodeImageView];
    self.qrcodeImageView = qrcodeImageView;
    [self addIntroduceButton];
    [self addShareButton];
    [self addInvitedCode];
    
    
}

-(void)addIntroduceButton{
    UIButton * introduceButton = [[UIButton alloc] initWithFrame:CGRectZero];
    introduceButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [introduceButton setTitle:@"规则介绍" forState:UIControlStateNormal];
    [introduceButton setTitleColor:HexColor(@"#666666") forState:UIControlStateNormal];
    [introduceButton setImage:[UIImage imageNamed:@"arrow_4"] forState:UIControlStateNormal];
    introduceButton.width = [introduceButton.currentTitle sizeInAre:MaxSize font:introduceButton.titleLabel.font].width + introduceButton.currentImage.size.width+5;
    introduceButton.height = introduceButton.currentImage.size.height;
    
    introduceButton.titleEdgeInsets = UIEdgeInsetsMake(0, -28, 0, 0);
    introduceButton.imageEdgeInsets = UIEdgeInsetsMake(0, 50, 0, 0);
    
    introduceButton.y = 15;
    introduceButton.x = self.view.width - introduceButton.width - 15;
    [self.view addSubview:introduceButton];
}

-(void)addShareButton{
    UIButton * shareButton = [[UIButton alloc] init];
    shareButton.titleLabel.font = [UIFont systemFontOfSize:18];
    shareButton.backgroundColor = HexColor(@"#3CB034");
    [shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareButton setTitle:@"推广赚绿豆" forState:UIControlStateNormal];
    shareButton.size = CGSizeMake(Ration_Share_width(165), Ration_Share_height(41));
    shareButton.centerX = self.view.width*0.5;
    shareButton.y = _containerView1.botton - shareButton.height*0.5-3;
    [shareButton addRadius:shareButton.height*0.5];
    [shareButton addTarget:self action:@selector(popShareView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareButton];
}

-(void)addInvitedCode{
    UILabel * l1 = [[UILabel alloc] init];
    l1.font = [UIFont systemFontOfSize:12];
    l1.textColor = HexColor(@"#666666");
    l1.text = @"您的邀请码是:";
    l1.size = [l1.text sizeInAre:MaxSize font:l1.font];
    l1.centerX = _containerView1.width*0.5;
    l1.y = self.qrcodeImageView.botton + 2*globalMargin;
    [_containerView1 addSubview:l1];
    
    UILabel * invitedCode = [[UILabel alloc] init];
    invitedCode.font = [UIFont systemFontOfSize:12];
    invitedCode.textColor = HexColor(@"#666666");
    invitedCode.text = @"f3ert";
    invitedCode.textAlignment = NSTextAlignmentCenter;
    invitedCode.size = CGSizeMake(_containerView1.width, 15);
    invitedCode.centerX = _containerView1.width*0.5;
    invitedCode.y = l1.botton + globalMargin*0.5;
    self.invitedCode = invitedCode;
    [_containerView1 addSubview:invitedCode];
    
    UILabel * playMethon = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    playMethon.numberOfLines = 0;
    playMethon.textColor = HexColor(@"#666666");
    [_containerView2 addSubview:playMethon];
    playMethon.font = [UIFont systemFontOfSize:12];
    NSMutableAttributedString * att = [[NSMutableAttributedString alloc] initWithString:@"玩法:邀请好友下载注册绿稀全球购客户端，即可获得好友购物佣金和500绿豆，好友可以获得500绿豆。"];
    [att addAttributes:@{NSForegroundColorAttributeName:HexColor(@"#666666")} range:NSMakeRange(0, att.length-6)];
    [att addAttributes:@{NSForegroundColorAttributeName:HexColor(@"#3CB034")} range:NSMakeRange(att.length-6, 6)];
    playMethon.attributedText = att;
    playMethon.size = CGSizeMake(_containerView2.width*0.8, _containerView2.botton - invitedCode.botton-Ration_Share_height(41)*0.5);
    playMethon.centerX = _containerView2.width * 0.5;
    playMethon.y = _containerView2.height-playMethon.height-globalMargin - Ration_Share_height(41)*0.5;
    [_containerView2 addSubview:playMethon];
    
}


-(void)pushToRulerController2{
//    LXMallRulerIntroduceController *ruleVc = [[LXMallRulerIntroduceController alloc] init];
//    ruleVc.imageName = @"rule_05";
//    ruleVc.desc = @"分佣规则";
//    [self.navigationController pushViewController:ruleVc animated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
-(void)popShareView{
    [self.shareView show];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"推广赚钱";
}

-(void)backButtonClick:(UIButton*)aButton{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)hiddenShareView{
    
    [self.shareView hidden];
}



-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

//    NSLog(@"%s %@",__func__,NSStringFromCGSize(self.view.frame.size));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
